package org.javagril.yuetong.service;

import org.apache.shiro.SecurityUtils;
import org.javagril.yuetong.bean.Carriers;
import org.javagril.yuetong.bean.RespPageBean;
import org.javagril.yuetong.bean.Scheduling;
import org.javagril.yuetong.bean.User;
import org.javagril.yuetong.mapper.CarriersMapper;
import org.javagril.yuetong.mapper.SchedulingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author QingtungTree
 * @Datetime: 2019/12/26
 * @Description:org.javagril.yuetong.service
 * @Version:
 */
@Service
public class SchedulingService {
    @Autowired /*对象注入*/
    SchedulingMapper schedulingMapper;

    @Autowired
    CarriersMapper carriersMapper;

    /*查询所有 调度任务信息*/
    public RespPageBean getSchedulingByPage(Integer page, Integer size){

        if(page != null && size != null){
               page = (page - 1)*size;
        }
        List<Scheduling> list = schedulingMapper.getSchedulingByPage(page,size);
        Long total=schedulingMapper.getTotal();
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(total);
        respPageBean.setData(list);
        return respPageBean;
    }


    /*调度分配*/
    public boolean addSchedule(Scheduling scheduling) {
        scheduling.setAltertime(new Date());
        scheduling.setCheckintime(new Date());
        scheduling.setFkUserid(((User) SecurityUtils.getSubject().getSession().getAttribute("user")).getUserid());
        scheduling.setIsdelete(-1);
        schedulingMapper.insertSelective(scheduling);
        Carriers record = new Carriers();
        record.setCarriersid(scheduling.getFkCarriersid()); /*拿到承运id*/
        record.setFinishedstate(1);
        return carriersMapper.updateByPrimaryKeySelective(record) == 1;
    }
}
